package com.voja.workoutrandomizer.repository;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Random;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.voja.AndroidUtils.Application;

import android.content.Context;

public class Workouts {
	
	public static final String EXERCISES_FILENAME = "exercises.json";
	public static final String WORKOUTS_FILENAME  = "workouts.json";
	
	private Context context;
	private JSONObject data;
	private HashMap<String, ArrayList<Exercise>> dataSet  = new HashMap<String, ArrayList<Exercise>>();
	private HashMap<String, ArrayList<String>>  dataFlat = new HashMap<String, ArrayList<String>>();
	
	public JSONObject loadJSONObject(String fileName) throws IOException, JSONException {
		String json = readAssetFile(fileName);
		return new JSONObject(json);
	}
	
	public JSONArray loadJSONArray(String fileName) throws IOException, JSONException {
		String json = readAssetFile(fileName);
		return new JSONArray(json);
	}
	
	public Workouts(Context context) throws IOException, JSONException {
		this.context = context;
		
		loadData();
	}
	
	public void loadData() throws IOException, JSONException {
		this.data = loadJSONObject(EXERCISES_FILENAME);
		dataFlatten();
	}
	
	public JSONArray getDefinitions() throws IOException, JSONException {
		return loadJSONArray(WORKOUTS_FILENAME);
	}
	
	private void dataFlatten() throws JSONException {
		Iterator<?> groupKeys = data.keys();
		String groupKey;
		ArrayList<String> flatGroup;
		while (groupKeys.hasNext()) {
			groupKey = groupKeys.next().toString();
			flatGroup = new ArrayList<String>();
		
			// flatten group
			JSONObject group = data.getJSONObject(groupKey);
			Iterator<?> keys = group.keys();
			JSONArray subGroup;
			String key;
			while (keys.hasNext()) {
				key = keys.next().toString();
				subGroup = group.getJSONArray(key);
				for (int i=0; i<subGroup.length(); i++) {
					flatGroup.add(subGroup.getString(i));
				}
			}
			
			// append group
			dataFlat.put(groupKey, flatGroup);
		}
	}
	
	public void randomize() {
		dataSet.put("Up",   randomizeGroup("Up"));
		dataSet.put("Down", randomizeGroup("Down"));
	}
	
	public ArrayList<Exercise> getGroup(String groupName) {
		return dataSet.get(groupName);
	}
	
	private ArrayList<Exercise> randomizeGroup(String groupName) {
		ArrayList<Exercise> groupDataSet = new ArrayList<Exercise>();
		ArrayList<String>  groupData    = dataFlat.get(groupName);
		
		Random r = new Random();
		for (int i=0; i<8; i++) {
			groupDataSet.add(new Exercise(groupData.get(r.nextInt(groupData.size()))));
		}
		
		return groupDataSet;
	}
	
	private String readAssetFile(String filename) throws IOException {
		InputStream    stream = context.getAssets().open(filename);
		BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
		
		String contents = "", line = "";
		while ((line = reader.readLine()) != null) {
			contents += line;
		}
		
		return contents;
	}
	
}
